$(function () {
    var settings = {
        url: ctx + "recording/list",
        pageSize: 5,
        pageList: [5, 10, 25, 50],
        clickToSelect: false,
        queryParams: function (params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                meetingId: $("#meetingId").val()
            };
        },
        columns: [{
            checkbox: true
        }, {
            field: 'recordID',
            visible: false
        }, {
            field: 'name',
            title: $MB.i18n('record.recordname')
        }, {
            field: 'published',
            title: $MB.i18n('record.ispublish'),
            formatter: function(value, row, index) {
                if(value=='true'){
                   return $MB.i18n('record.published');
                }else{
                   return $MB.i18n('record.unpublished');
                }
            }
        }
        , {
            field: 'startTime',
            title: $MB.i18n('record.starttime'),
            formatter: function(value, row, index) {
               return util.common.getDateStr(parseInt(value)/1000);
            }
        }, {
            field: 'endTime',
            title: $MB.i18n('record.endtime'),
            formatter: function(value, row, index) {
                return util.common.getDateStr(parseInt(value)/1000);
            }
        }, {
            title: $MB.i18n('record.link'),
            formatter: function(value, row, index) {
//               console.log(row);
               var url=row.playback.format.url;
//               console.log(url);
               var btnTxt=$MB.i18n('record.view');
               var retHtml='<a target="_blank" href="'+url+'" class="recording-preview btn btn-primary  btn-save" >'+btnTxt+'</a>';
               if(row.published=='false'){
                   retHtml='<a href="#" class="recording-preview btn btn-default" >'+btnTxt+'</a>';
               }
               return [retHtml].join("");
            }
        }, {
              title: $MB.i18n('record.operate'),
              formatter: function(value, row, index) {
                 var btnName=$MB.i18n('record.publishedbtn');
                 var retHtml='<button type="button" class="record-publish btn btn-primary" onclick="toggleRecordingPublish(true,\''+row.recordID+'\')" style="margin-right:15px;width:80px">'+btnName+'</button>';
                 if(row.published=='true'){
                     btnName=$MB.i18n('record.unpublishedbtn');
                     retHtml='<button type="button" class="record-publish btn btn-primary" onclick="toggleRecordingPublish(false,\''+row.recordID+'\')" style="margin-right:15px;width:80px">'+btnName+'</button>';
                     var url=row.playback.format.url;
                     //download：https://< BBB server url >/download/presentation/ <internal-meeting-id> /<internal-meeting-id>.mp4
                     url=url.split("/playback/")[0];
                     var downloadTxt=$MB.i18n('record.download');
                     console.log(url);
                     var internalMeetingID=row.internalMeetingID;
                     var downloadUrl=url+"/download/presentation/"+internalMeetingID+"/"+internalMeetingID+".mp4";
                     retHtml +='<a target="_blank" href="'+downloadUrl+'" class="recording-preview btn btn-primary" >'+downloadTxt+'</a>';
                 }
                 return [retHtml].join("");
              }
        }]
    };

    $MB.initTable('recordingTable', settings);
});

function deleteRecordings() {
    var selected = $("#recordingTable").bootstrapTable('getSelections');
    var selected_length = selected.length;
    var contain = false;
    if (!selected_length) {
        $MB.n_warning($MB.i18n('record.selectrecord'));
        return;
    }
    var selectIds= new Array();
    for (var i = 0; i < selected_length; i++) {
        selectIds=selectIds.concat(selected[i].recordID);
    }

    var ids=selectIds.join(",");
    $MB.confirm({
        text: $MB.i18n('record.deltxt'),
        confirmButtonText: $MB.i18n('meetingroom.confirm')
    }, function() {
        $.post(ctx + 'recording/delete', { "ids": ids }, function(r) {
            if (r.code === 0) {
                $MB.n_success(r.msg);
                $MB.refreshTable('recordingTable');
            } else {
                $MB.n_danger(r.msg);
            }
        });
    });
}

function toggleRecordingPublish(publish,recordId) {
    $.post(ctx + 'recording/publish', { "recordId": recordId,"publish": publish }, function(r) {
        if (r.code === 0) {
            $MB.n_success(r.msg);
            $MB.refreshTable('recordingTable');
        } else {
            $MB.n_danger(r.msg);
        }
    });
}